package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysFillQuestionDao;
import com.groupfifteen.entity.SysFillQuestionEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysFillQuestionService")
@RequiredArgsConstructor
public class SysFillQuestionServiceImpl extends ServiceImpl<SysFillQuestionDao, SysFillQuestionEntity> implements SysFillQuestionService {

    private final SysFillQuestionDao sysFillQuestionDao;

    @Override
    public IPage<SysFillQuestionEntity> QuerySysFillQuestions(SysFillQuestionEntity sysFillQuestionEntity, int pageNo, int pageSize) {
        LambdaQueryWrapper<SysFillQuestionEntity> query = Wrappers.lambdaQuery();
        // 假设我们根据questionId进行查询，您可以根据实际需求添加更多的查询条件
        query.eq(sysFillQuestionEntity.getQuestionId() != null, SysFillQuestionEntity::getQuestionId, sysFillQuestionEntity.getQuestionId());

        // 确保页码和页大小有效
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }

        // 创建分页对象
        IPage<SysFillQuestionEntity> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        return baseMapper.selectPage(page, query);
    }

//    @Override
//    public List<Integer> findBySubject(String subject, Integer pageNo) {
//        return sysFillQuestionDao.findBySubject(subject,pageNo);
//    }
//
    @Override
    public Integer AddFillQuestion(SysFillQuestionEntity sysFillQuestionEntity) {
        return baseMapper.insert(sysFillQuestionEntity);
    }

    @Override
    public Integer DeleteFillQuestion(Integer question_id) {
        return baseMapper.deleteById(question_id);
    }

    @Override
    public SysFillQuestionEntity GetFillQuestion(Integer question_id) {
        return baseMapper.selectById(question_id);
    }

    @Override
    public Integer UpdateFillQuestion(SysFillQuestionEntity sysFillQuestionEntity) {
        return baseMapper.updateById(sysFillQuestionEntity);
    }
    /**
     * 通过试卷id查询问题
     *
     * @param paperId 试卷id
     * @return 问题
     */
    @Override
    public List<SysFillQuestionEntity> findByIdAndType(Integer paperId) {
        return sysFillQuestionDao.findByIdAndType(paperId);
    }

    /**
     * 查询最后一条填空题
     *
     * @return 最后一条填空题
     */
    @Override
    public SysFillQuestionEntity findOnlyQuestionId() {
        return baseMapper.findOnlyQuestionId();
    }

//    /**
//     * 添加填空题
//     *
//     * @param fillQuestion 填空题内容
//     * @return 结果
//     */
//    @Override
//    public Integer add(SysFillQuestionEntity fillQuestion) {
//        return sysFillQuestionDao.add(fillQuestion);
//    }

    /**
     * 通过学科查询
     *
     * @param subject 学科查询
     * @param pageNo 分页数
     * @return 题目
     */
    @Override
    public List<Integer> findBySubject(String subject, Integer pageNo) {
        return sysFillQuestionDao.findBySubject(subject,pageNo);
    }

    /**
     * 编辑填空题
     *
     * @param fillQuestion 填空题内容
     * @return 结果
     */
//    @Override
//    public Integer edit(SysFillQuestionEntity fillQuestion) {
//        return sysFillQuestionDao.edit(fillQuestion);
//    }
}